<?php
/**
 * 签约业绩model
 * @author wangwx 2015-05-18
 */
namespace common\models\myhtsq;

use common\repositories\myhtsq\ErpUserRepository;
use Yii;
use common\models\BaseModel;

/**
 * This is the model class for table "myscrm_sign_achievement".
 *
 * @property string $achievement_id
 * @property string $contract_id
 * @property string $achievement_amount
 * @property string $belong_by
 * @property string $abu_by
 * @property string $department_by
 * @property string $area_by
 * @property string $manager_by
 * @property string $created_on
 * @property string $created_by
 * @property string $modified_on
 * @property string $modified_by
 */
class SignAchievementModel extends BaseModel
{
	public $achievement_id; // 业绩ID
	public $contract_id; // 合同ID
	public $achievement_amount; // 签约业绩金额= 签约金额
	public $belong_by; // 业绩归属人ID
	public $abu_by; // 业绩归属ABU
	public $department_by; // 业绩归属部门
	public $area_by; // 业绩归属区域
	public $manager_by; // ERP客户经理
	public $created_on; // 创建时间
	public $created_by; // 创建人
	public $modified_on; // 修改时间
	public $modified_by;//修改人

    public $achievement_belong_time;//业绩归属时间=合同录入时间
    public $erp_achievement_belong_bu;//ERP客户经理所属部门
    public $erp_achievement_belong;//ERP业绩归属人
    public $erp_achievement_belong_abu;//ERP业绩归属ABU（由ERP业绩归属人带出）
    public $erp_achievement_belong_abu_owner;//ERP业绩归属ABU负责人
    public $erp_achievement_belong_bu_manager;//ERP业绩归属部门经理
    
    public $attribute_name; //属性名称

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        	//新	增
            [['achievement_amount'], 'number','on'=>'add'],
            [['created_on', 'modified_on','achievement_belong_time'], 'safe','on'=>'add'],
        	['achievement_amount', 'default', 'value' => 0.00,'on'=>'add'],
            [['achievement_id', 'contract_id', 'belong_by', 'abu_by', 'department_by', 'area_by', 'manager_by',
                'created_by', 'modified_by',
                'erp_achievement_belong_bu','erp_achievement_belong','erp_achievement_belong_abu','erp_achievement_belong_abu_owner',
                'erp_achievement_belong_bu_manager','attribute_name'
             ], 'string', 'max' => 36,'on'=>'add'],
        		
        	//更新
        	[['achievement_id'], 'required','on'=>'update'],
        	[['achievement_amount'], 'number','on'=>'update'],
        	[['created_on', 'modified_on','achievement_belong_time'], 'safe','on'=>'update'],
        	[['achievement_id', 'contract_id', 'belong_by', 'abu_by', 'department_by', 'area_by', 'manager_by',
                'created_by', 'modified_by',
                'erp_achievement_belong_bu','erp_achievement_belong','erp_achievement_belong_abu','erp_achievement_belong_abu_owner',
                'erp_achievement_belong_bu_manager','attribute_name'
             ], 'string', 'max' => 36,'on'=>'update'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'achievement_id' => '业绩ID',
            'contract_id' => '合同ID',
            'achievement_amount' => '签约业绩金额= 签约金额',
            'belong_by' => '业绩归属人ID',
            'abu_by' => '业绩归属ABU',
            'department_by' => '业绩归属部门',
            'area_by' => '业绩归属区域',
            'manager_by' => 'ERP客户经理',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人',
            'achievement_belong_time' => '业绩归属时间=合同录入时间',
            'erp_achievement_belong_bu' => 'ERP客户经理所属部门',
            'erp_achievement_belong' => 'ERP业绩归属人',
            'erp_achievement_belong_abu' => 'ERP业绩归属ABU（由ERP业绩归属人带出）',
            'erp_achievement_belong_abu_owner' => 'ERP业绩归属ABU负责人',
            'erp_achievement_belong_bu_manager' => 'ERP业绩归属部门经理',
        ];
    }

    /*
     * 自动插入时,根据用户填充组织信息,因为调整用户组织后,不能修改合同归属信息,因此修改业务信息时,不能调用此方法.
     */
    public function fillBuInfoByUser()
    {
        //业绩归属人相关
        if(!empty($this->belong_by)) {
            $belongUser = ErpUserRepository::getUserById($this->belong_by);
            $this->department_by = $belongUser->DepartmentGUID;
        }

        //ERP业绩归属人相关
        if(!empty($this->erp_achievement_belong)) {
            $erpBelongUser = ErpUserRepository::getUserById($this->erp_achievement_belong);
            $this->erp_achievement_belong_abu = $erpBelongUser->AbuGUID;
            $this->erp_achievement_belong_abu_owner = $erpBelongUser->ABuFzrID;
            $this->erp_achievement_belong_bu_manager = $erpBelongUser->DeptmentFzrID;
        }
        //ERP业绩归属客户经理相关
        if(!empty($this->manager_by)){
            $manageUser = ErpUserRepository::getUserById($this->manager_by);
            $this->erp_achievement_belong_bu = $manageUser->DepartmentGUID;
        }
    }

}
